/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2008-2011, Salzburg Research. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * - Neither the name of the KiWi Project nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributor(s):
 * sschaffe
 *
 * File Info:
 * User: sschaffe
 * Date: 2011/01/03
 * Time: 16:57
 *
 * Project: KiWi2, kiwi-core module
 */

package kiwi.core.api.transaction;

import javax.enterprise.inject.Alternative;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

/**
 * Transactions cover a unit of work in the KiWi system. The KiWi transaction management system considers
 * transactions as a kind of journal where all data will be stored before it is actually committed to the
 * database. Transactions are managed by the TransactionService.
 *
 * <p/>
 * User: sschaffe
 */
@Alternative
public class Transaction {

    /**
     * The identifier of the transaction. A randomly generated UUID. Used for internal management purposes.
     */
    private String id;

    /**
     * The data covered by this transaction. Data contained here will be committed to the database upon
     * transaction commit.
     */
    private TransactionData data;

    /**
     * indicates whether the transaction should be automatically committed. Automatic commit will be checked
     * at the latest when a request is finished or when the timeout occurs;
     */
    private boolean autoCommit;

    /**
     * Transaction timeout. Transactions will be considered invalid after the number of milliseconds indicated
     * by this variable. Timeouts are checked by a periodic thread that is executed by the TransactionService
     */
    private long timeout;


    /**
     * Transactions to wait for before this transaction can be committed.
     */
    private Set<Transaction> dependsOn;




    /**
     * The state of the transaction. Only transactions that are of state ACTIVE can be used to add additional
     * data to.
     */
    private TransactionState state;

    public Transaction() {
        data      = new TransactionData();
        dependsOn = new HashSet<Transaction>();
        state     = Transaction.TransactionState.CREATED;
        id        = UUID.randomUUID().toString();
        timeout   = 60000L;
    }



    public TransactionData getData() {
        return data;
    }

    public void setData(TransactionData data) {
        this.data = data;
    }


    public void setState(TransactionState state) {
        this.state = state;
    }

    public TransactionState getState() {
        return state;
    }

    public boolean isCreated() {
        return state == Transaction.TransactionState.CREATED;
    }

    public boolean isActive() {
        return state == Transaction.TransactionState.ACTIVE;
    }

    public boolean isCommitted() {
        return state == Transaction.TransactionState.COMMITTED;
    }

    public boolean isRolledBack() {
        return state == Transaction.TransactionState.ROLLEDBACK;
    }

    public boolean isAborted() {
        return state == Transaction.TransactionState.ABORTED;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public boolean isAutoCommit() {
        return autoCommit;
    }

    public void setAutoCommit(boolean autoCommit) {
        this.autoCommit = autoCommit;
    }

    public long getTimeout() {
        return timeout;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }


    public Set<Transaction> getDependsOn() {
        return dependsOn;
    }

    public void setDependsOn(Set<Transaction> dependsOn) {
        this.dependsOn = dependsOn;
    }

    @Override
    public String toString() {
        return "Transaction{" +
                "id='" + id + '\'' +
                ", state=" + state +
                '}';
    }

    public static enum TransactionState {
        CREATED,
        ACTIVE,
        COMMITTING,
        COMMITTED,
        ROLLINGBACK,
        ROLLEDBACK,
        ABORTED
    }
}
